package com.symaster.common.swing.dialog.selectfile;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.symaster.common.swing.element.list.SwingListPanel;
import com.symaster.common.swing.style.Style;
import com.symaster.common.swing.util.SwingUtil;
import com.symaster.common.swing.util.ThreadPoolUtil;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

public class SwingSelectFileDialog extends JDialog {

    private static final ImageIcon home = SwingUtil.loadIconEnabledCache("home.png", 15, 15);
    private static final ImageIcon newFolder = SwingUtil.loadIcon("new_folder.png", 15, 15);
    private static final ImageIcon deleteFolder = SwingUtil.loadIconEnabledCache("delete_folder.png", 15, 15);
    private static final ImageIcon reloadFolder = SwingUtil.loadIconEnabledCache("reload.png", 15, 15);
    private static final String FILE_TYPE_DEFAULD = "$defauld$";
    private static final String FILE_TYPE_FOLDER = "$folder$";
    private static final String FILE_TYPE_DRIVE = "$drive$";
    private static final String FILE_TYPE_JPG = "jpg";
    private static final String FILE_TYPE_PNG = "png";
    private static final String FILE_TYPE_BMP = "bmp";
    private final String[][] fileTypes;
    private SwingListPanel swingListPanel;
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JButton to_home_button;
    private JButton new_folder;
    private JButton delete_folder;
    private JButton folder_reload;
    private JTextField pathInput;
    private JTextField fileInput;
    private JComboBox comboBox1;
    private JScrollPane sp;
    private final Integer width;
    private final Integer height;
    private final String defaultPath;

    public SwingSelectFileDialog(String[][] fileTypes) {
        this.fileTypes = fileTypes;
        this.width = 600;
        this.height = 900;
        this.defaultPath = "C:";
        init();
    }

    /**
     * Creates a modeless dialog without a title and without a specified
     * {@code Frame} owner.  A shared, hidden frame will be
     * set as the owner of the dialog.
     * <p>
     * This constructor sets the component's locale property to the value
     * returned by {@code JComponent.getDefaultLocale}.
     * <p>
     * NOTE: This constructor does not allow you to create an unowned
     * {@code JDialog}. To create an unowned {@code JDialog}
     * you must use either the {@code JDialog(Window)} or
     * {@code JDialog(Dialog)} constructor with an argument of
     * {@code null}.
     *
     * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()}
     *                           returns {@code true}.
     * @see GraphicsEnvironment#isHeadless
     * @see JComponent#getDefaultLocale
     */
    public SwingSelectFileDialog(String[][] fileTypes, Integer width, Integer height) {
        this.fileTypes = fileTypes;
        this.width = width;
        this.height = height;
        this.defaultPath = "C:";
        init();
    }

    public SwingSelectFileDialog(String[][] fileTypes, Integer width, Integer height, String defaultPath) {
        this.fileTypes = fileTypes;
        this.width = width;
        this.height = height;

        int i1 = defaultPath.indexOf(":");
        String driveName = defaultPath.substring(0, i1 + 1) + File.separator;
        System.out.println("driveName = " + driveName);
        File[] roots = File.listRoots();
        boolean flag = false;
        for (int i = 0; i < roots.length; i++) {
            String path = roots[i].getPath();
            if (path.equals(driveName)) {
                flag = true;
            }
        }
        if (!flag) {
            defaultPath = "C:" + File.separator;
        }
        this.defaultPath = new File(defaultPath).getPath();
        init();
    }

    private void initFileList() {
        // defaultPath = getFilePath();
        pathInput.setText(defaultPath);

        ThreadPoolUtil.execute(() -> {
            File[] roots = File.listRoots();
            for (int i = 0; i < roots.length; i++) {
                //                swingListPanel.put(new FolderItem(roots[i].getPath()));
            }
        });
    }

    public void selectFilePath(String FilePath) {

    }

    private void init() {

        setMinimumSize(new Dimension(300, 400));
        setTitle("选择文件");
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        swingListPanel = new SwingListPanel(460, 30, new Color(25, 30, 33), 0, 0);
        sp.setViewportView(swingListPanel);
        initIcon();
        initComboBox();
        initLayout();
        initBorder();
        initFont();
        initListener();
        initFileList();
        // comboBox1.addItem("plf文件");

        Point point = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
        this.setBounds(point.x - width / 2, point.y - height / 2, width, height);
        this.pack();
    }

    private void initComboBox() {
        for (String[] fileType : fileTypes) {
            comboBox1.addItem(fileType[1]);
        }
    }

    private void initLayout() {
        setPreferredSize(new Dimension(width, height));
    }

    private void initListener() {
        buttonOK.addActionListener(e -> onOK());
        buttonCancel.addActionListener(e -> onCancel());
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });
    }

    private void initBorder() {
        Style.setBorder(Style.BORDER_EMPTY, pathInput, fileInput, buttonOK, buttonCancel, to_home_button, comboBox1, new_folder, delete_folder, folder_reload);

        Component[] components = comboBox1.getComponents();
        for (Component value : components) {
            try {
                JComponent component = (JComponent) value;
                Style.setBorder(Style.BORDER_EMPTY, component);
            } catch (Exception ignored) {
            }

            try {
                AbstractButton component2 = (AbstractButton) value;
                component2.setFocusPainted(false);
            } catch (Exception ignored) {
            }
        }
    }

    private void initFont() {
        Style.setFont(1, buttonOK, buttonCancel, pathInput, fileInput, comboBox1);
    }

    /**
     * Sets the preferred size of this component to a constant
     * value.  Subsequent calls to <code>getPreferredSize</code> will always
     * return this value.  Setting the preferred size to <code>null</code>
     * restores the default behavior.
     *
     * @param preferredSize The new preferred size, or null
     * @see #getPreferredSize
     * @see #isPreferredSizeSet
     * @since 1.5
     */
    @Override
    public void setPreferredSize(Dimension preferredSize) {
        super.setPreferredSize(preferredSize);
        contentPane.setPreferredSize(preferredSize);
    }

    private void onOK() {
        // add your code here
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    private void initIcon() {
        to_home_button.setIcon(home);
        new_folder.setIcon(newFolder);
        delete_folder.setIcon(deleteFolder);
        folder_reload.setIcon(reloadFolder);
    }

    {
        // GUI initializer generated by IntelliJ IDEA GUI Designer
        // >>> IMPORTANT!! <<<
        // DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPane = new JPanel();
        contentPane.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1));
        contentPane.setBackground(new Color(-14802394));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(1, 4, new Insets(0, 0, 0, 0), -1, -1));
        panel1.setBackground(new Color(-14802394));
        contentPane.add(panel1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, 1, null, null, null, 0, false));
        final Spacer spacer1 = new Spacer();
        panel1.add(spacer1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1, true, false));
        panel2.setBackground(new Color(-14802394));
        panel1.add(panel2, new GridConstraints(0, 3, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        panel2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        buttonOK = new JButton();
        buttonOK.setBackground(new Color(-13024950));
        buttonOK.setFocusPainted(false);
        buttonOK.setForeground(new Color(-2431246));
        buttonOK.setText("OK");
        panel2.add(buttonOK, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        buttonCancel = new JButton();
        buttonCancel.setBackground(new Color(-13024950));
        buttonCancel.setFocusPainted(false);
        buttonCancel.setForeground(new Color(-2431246));
        buttonCancel.setText("Cancel");
        panel2.add(buttonCancel, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new BorderLayout(0, 0));
        panel1.add(panel3, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, new Dimension(150, -1), null, 0, false));
        panel3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        fileInput = new JTextField();
        fileInput.setBackground(new Color(-15131103));
        fileInput.setCaretColor(new Color(-425364));
        fileInput.setDisabledTextColor(new Color(-9800835));
        fileInput.setForeground(new Color(-1973016));
        panel3.add(fileInput, BorderLayout.CENTER);
        final JPanel panel4 = new JPanel();
        panel4.setLayout(new BorderLayout(0, 0));
        panel1.add(panel4, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, new Dimension(75, -1), null, 0, false));
        panel4.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        comboBox1 = new JComboBox();
        comboBox1.setBackground(new Color(-15131103));
        comboBox1.setEditable(false);
        comboBox1.setEnabled(true);
        comboBox1.setForeground(new Color(-1973788));
        comboBox1.setPreferredSize(new Dimension(140, 25));
        panel4.add(comboBox1, BorderLayout.CENTER);
        final JPanel panel5 = new JPanel();
        panel5.setLayout(new BorderLayout(0, 0));
        panel5.setBackground(new Color(-14802394));
        contentPane.add(panel5, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        panel5.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        final JPanel panel6 = new JPanel();
        panel6.setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel6.setBackground(new Color(-14802394));
        panel6.setPreferredSize(new Dimension(0, 70));
        panel5.add(panel6, BorderLayout.NORTH);
        panel6.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        final JPanel panel7 = new JPanel();
        panel7.setLayout(new GridLayoutManager(1, 7, new Insets(0, 0, 0, 15), -1, -1));
        panel7.setBackground(new Color(-14802394));
        panel6.add(panel7, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, new Dimension(-1, 25), null, 0, false));
        panel7.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        to_home_button = new JButton();
        to_home_button.setBackground(new Color(-14802394));
        to_home_button.setFocusPainted(false);
        to_home_button.setPreferredSize(new Dimension(25, 25));
        to_home_button.setText("");
        panel7.add(to_home_button, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, new Dimension(25, 25), null, 0, false));
        final Spacer spacer2 = new Spacer();
        panel7.add(spacer2, new GridConstraints(0, 6, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        final JPanel panel8 = new JPanel();
        panel8.setLayout(new BorderLayout(0, 0));
        panel8.setBackground(new Color(-13815498));
        panel7.add(panel8, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, new Dimension(1, 23), null, 0, false));
        panel8.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        new_folder = new JButton();
        new_folder.setBackground(new Color(-14802394));
        new_folder.setFocusPainted(false);
        new_folder.setText("");
        panel7.add(new_folder, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(25, 25), null, 0, false));
        final JPanel panel9 = new JPanel();
        panel9.setLayout(new BorderLayout(0, 0));
        panel9.setBackground(new Color(-13815498));
        panel7.add(panel9, new GridConstraints(0, 4, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, new Dimension(1, 23), null, 0, false));
        panel9.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        delete_folder = new JButton();
        delete_folder.setBackground(new Color(-14802394));
        delete_folder.setFocusPainted(false);
        delete_folder.setText("");
        panel7.add(delete_folder, new GridConstraints(0, 3, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(25, 25), null, 0, false));
        folder_reload = new JButton();
        folder_reload.setBackground(new Color(-14802394));
        folder_reload.setFocusPainted(false);
        folder_reload.setText("");
        panel7.add(folder_reload, new GridConstraints(0, 5, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(25, 25), null, 0, false));
        final JPanel panel10 = new JPanel();
        panel10.setLayout(new GridLayoutManager(1, 1, new Insets(0, 5, 0, 5), -1, -1));
        panel10.setBackground(new Color(-15131103));
        panel6.add(panel10, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_SOUTH, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, new Dimension(-1, 35), null, 0, false));
        panel10.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        pathInput = new JTextField();
        pathInput.setBackground(new Color(-15131103));
        pathInput.setCaretColor(new Color(-425364));
        pathInput.setDisabledTextColor(new Color(-9800835));
        pathInput.setForeground(new Color(-1973016));
        pathInput.setSelectedTextColor(new Color(-1));
        pathInput.setSelectionColor(new Color(-13946826));
        panel10.add(pathInput, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(-1, 35), null, 0, false));
        final JPanel panel11 = new JPanel();
        panel11.setLayout(new BorderLayout(0, 0));
        panel11.setBackground(new Color(-14802394));
        panel5.add(panel11, BorderLayout.CENTER);
        panel11.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        sp = new JScrollPane();
        panel11.add(sp, BorderLayout.CENTER);
        sp.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        final JPanel panel12 = new JPanel();
        panel12.setLayout(new BorderLayout(0, 0));
        panel12.setBackground(new Color(-425364));
        panel12.setPreferredSize(new Dimension(0, 2));
        panel11.add(panel12, BorderLayout.NORTH);
    }

    /** @noinspection ALL */
    public JComponent $$$getRootComponent$$$() {
        return contentPane;
    }

}
